#!/bin/bash

# HiTLS unified test runner script
# This script demonstrates various test scenarios

echo "HiTLS 统一测试脚本"
echo "=================="

# Check if the test program exists
if [ ! -f "./altcp_hitls_test" ]; then
    echo "错误: 测试程序 altcp_hitls_test 不存在"
    echo "请先运行: make -f Makefile.hitls_test"
    exit 1
fi

# Check if certificate files exist
if [ ! -d "./files" ]; then
    echo "警告: 证书文件目录 ./files 不存在"
    echo "请确保证书文件已正确放置在 testcode/files/ 目录中"
    echo "参考 files/README.md 了解所需文件"
fi

# Function to run test with timeout
run_test() {
    local name="$1"
    local args="$2"
    local timeout="10"
    
    echo ""
    echo "运行测试: $name"
    echo "参数: $args"
    echo "命令: ./altcp_hitls_test $args"
    echo "提示: 按 Ctrl+C 停止测试"
    echo "----------------------------------------"
    
    timeout $timeout ./altcp_hitls_test $args &
    local pid=$!
    
    # Wait for a few seconds then kill if still running
    sleep 5
    if kill -0 $pid 2>/dev/null; then
        echo "测试仍在运行中... (将在 $timeout 秒后自动停止)"
        wait $pid
    fi
    
    echo "测试完成: $name"
    echo "========================================"
}

# Display menu
while true; do
    echo ""
    echo "请选择测试场景:"
    echo "1. 服务器模式 - 未加密私钥，无CRL"
    echo "2. 服务器模式 - 加密私钥，无CRL"
    echo "3. 服务器模式 - 加密私钥，使用CRL"
    echo "4. 客户端模式 - 未加密私钥，无认证，无CRL"
    echo "5. 客户端模式 - 加密私钥，无认证，无CRL"
    echo "6. 客户端模式 - 加密私钥，要求认证，使用CRL"
    echo "7. 显示程序用法"
    echo "8. 退出"
    echo ""
    read -p "请输入选择 (1-8): " choice

    case $choice in
        1)
            run_test "服务器-未加密-无CRL" "server 0 0 0"
            ;;
        2)
            run_test "服务器-加密-无CRL" "server 0 0 1"
            ;;
        3)
            run_test "服务器-加密-CRL" "server 1 0 1"
            ;;
        4)
            run_test "客户端-未加密-无认证-无CRL" "client 0 0 0"
            ;;
        5)
            run_test "客户端-加密-无认证-无CRL" "client 0 0 1"
            ;;
        6)
            run_test "客户端-加密-认证-CRL" "client 1 1 1"
            ;;
        7)
            echo ""
            echo "程序用法:"
            ./altcp_hitls_test
            ;;
        8)
            echo "退出测试脚本"
            exit 0
            ;;
        *)
            echo "无效选择，请输入 1-8"
            ;;
    esac
done 